<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-card>
        <div slot="header" class="clearfix">
          <span style="font-weight:bold;color:#1890FF;font-size:14px;">查询条件设置</span>
        </div>
        <el-form-item label="客户端IP" prop="serverIp">
          <el-input
            v-model="queryParams.serverIp"
            placeholder="请输入客户端IP"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="机器名" prop="hostName">
          <el-input
            v-model="queryParams.hostName"
            placeholder="请输入机器名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-card>
    </el-form>

    <el-card style="margin-top:10px;">
      <div slot="header" class="clearfix">
        <span style="font-weight:bold;color:#1890FF;font-size:14px;">集群内客户端列表</span>
      </div>
      <el-row>
        <el-col :span="24">
          <el-button type="success" size="mini" @click="startMultiEngines">启动引擎</el-button>
          <el-button type="danger" size="mini" @click="stopMultiEngines">停止引擎</el-button>
          <el-button type="warning" size="mini" @click="restartMultiEngines">重启引擎</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" border :data="engineList" style="margin-top:10px;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户IP" align="center" prop="serverIp" />
        <el-table-column label="机器名" align="center" prop="hostName" />
        <el-table-column label="引擎类型" align="center" prop="engineType" />
        <el-table-column label="引擎类型" align="center" prop="typeName" width="150" />
        <el-table-column label="引擎状态" align="center" prop="engineStatus" width="120" />
        <el-table-column label="加入集群时间" align="center" prop="inTime" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.inTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-s-promotion"
              @click="handleUpdate(scope.row)"
            >客户端操作</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改引擎信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务器IP" prop="serverIp">
          <el-input v-model="form.serverIp" placeholder="请输入服务器IP" />
        </el-form-item>
        <el-form-item label="机器名" prop="hostName">
          <el-input v-model="form.hostName" placeholder="请输入机器名" />
        </el-form-item>
        <el-form-item label="开机状态" prop="onOff">
          <el-input v-model="form.onOff" placeholder="请输入开机状态" />
        </el-form-item>
        <el-form-item label="引擎类型" prop="typeName">
          <el-input v-model="form.typeName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工作路径" prop="jobPath">
          <el-input v-model="form.jobPath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="${comment}" prop="clusterIp">
          <el-input v-model="form.clusterIp" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="是否在集群内" prop="clusterIn">
          <el-input v-model="form.clusterIn" placeholder="请输入是否在集群内" />
        </el-form-item>
        <el-form-item label="加入集群时间" prop="inTime">
          <el-date-picker clearable
            v-model="form.inTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择加入集群时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退出集群时间" prop="quitTime">
          <el-date-picker clearable
            v-model="form.quitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退出集群时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEngine, getEngine, delEngine, addEngine, updateEngine } from "@/api/system/engine";
import axios from "axios";

export default {
  name: "Engine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引擎信息表表格数据
      engineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serverIp: null,
        hostName: null,
        onOff: null,
        engineStatus: null,
        engineType: null,
        typeName: null,
        jobPath: null,
        serverType: null,
        clusterIp: null,
        clusterIn: 1,
        inTime: null,
        quitTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    startMultiEngines(){
      if(this.ids.length>0){
        const that = this;
        this.$modal.confirm('是否确认启动引擎？').then(function() {
          for(let i=0;i<that.ids.length;i++){
            let data = {
              operation: "start"
            }
            let path = "http://" + that.ids[i] + ":9500/operateEngineAction";
            axios
            .post(path,data)
            .then((data) => {
            })
          }
          that.getList();
        }).then(() => {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        }).catch(() => {});
      }else{
        this.$message.error("请选择操作的客户端!");
      }
    },
    stopMultiEngines(){
      if(this.ids.length>0){
        const that = this;
        this.$modal.confirm('是否确认停止引擎？').then(function() {
          for(let i=0;i<that.ids.length;i++){
            let data = {
              operation: "stop"
            }
            let path = "http://" + that.ids[i] + ":9500/operateEngineAction";
            axios
            .post(path,data)
            .then((data) => {
            })
          }
          that.getList();
        }).then(() => {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        }).catch(() => {});
      }else{
        this.$message.error("请选择操作的客户端!");
      }
    },
    restartMultiEngines(){
      if(this.ids.length>0){
        const that = this;
        this.$modal.confirm('是否确认重启引擎？').then(function() {
          for(let i=0;i<that.ids.length;i++){
            let data = {
              operation: "restart"
            }
            let path = "http://" + that.ids[i] + ":9500/operateEngineAction";
            axios
            .post(path,data)
            .then((data) => {
            })
          }
          that.getList();
        }).then(() => {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        }).catch(() => {});
      }else{
        this.$message.error("请选择操作的客户端!");
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jobId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 查询引擎信息表列表 */
    getList() {
      this.loading = true;
      listEngine(this.queryParams).then(response => {
        this.engineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serverIp: null,
        hostName: null,
        onOff: null,
        engineStatus: null,
        engineType: null,
        typeName: null,
        jobPath: null,
        serverType: null,
        clusterIp: null,
        clusterIn: null,
        inTime: null,
        quitTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serverIp)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加引擎信息表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: "/clientdetail",
        query: {
          serverIp: row.serverIp,
          hostName: row.hostName,
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serverIp != null) {
            updateEngine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEngine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serverIps = row.serverIp || this.ids;
      this.$modal.confirm('是否确认删除引擎信息表编号为"' + serverIps + '"的数据项？').then(function() {
        return delEngine(serverIps);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/engine/export', {
        ...this.queryParams
      }, `engine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
